/* file: ND_memory.h
 * define memory manager 
 * version 1.0
 * neil
 * 2005-11-15
 */

#ifndef _ND_MEMORY_H_
#define _ND_MEMORY_H_

#include "nd_common/nd_common.h"
#include "nd_common/nd_mempool.h"

#define USER_ND_MALLOC		1

#ifdef USER_ND_MALLOC

#ifdef ND_IMPLETE_MEMPOOL
#error can not include this file in current c-file
#endif 
#undef free 
#undef malloc

#ifdef ND_DEBUG
#include "nd_common/nd_dbg.h"

static __INLINE__ void *nd_malloc(size_t size, NDINT8 *file, NDINT32 line)
{
	void *p = nd_alloc_check(size,nd_global_alloc )  ;
	if(p) {
		_source_log(p,(NDINT8 *)"malloc",(NDINT8 *)"memory leak!", file,line) ;
	}
	return p ;
}

static __INLINE__ void nd_free(void *p)
{
	if(p) {
		NDINT32 ret =_source_release(p) ;
		nd_assert(0==ret) ;
		nd_free_check(p,nd_global_free ) ;
	}
}

#define malloc(__n)		nd_malloc(__n,__FILE__, __LINE__) 
#define free(__p)		nd_free(__p)
//#define malloc_l(__n)	nd_global_alloc_l(__n) 
//#define free_l(__p,__n) nd_global_free_l(__p,__n)

#else 
#define malloc(__n)		nd_global_alloc(__n) 
#define free(__p)		nd_global_free(__p)
//#define malloc_l(__n)	nd_global_alloc_l(__n) 
//#define free_l(__p,__n) nd_global_free_l(__p,__n)
#endif			//debug

#else 
#endif	//	USER_ND_MALLOC

#endif
